import requests
import json
import re
def get_moudleID(moudle_type):
	moudle_dict={
		"moudle":{"EMP":"员工"},
		"subMoudle":{"JOB-M":"职务信息","JOB-P":"兼岗信息","ACC":"创建账号","BASE":"添加员工","TA":"人才库信息",},
		"type":{"ADD":"新增","UPDATE":"更新","COVER":"更新数据","DELETE":"删除",},
		"rNo":{"CORE_EMP_MAINJOB_IMP":"职务信息","CORE_EMP_PTJOB_IMP":"兼岗信息","CORE_EMP_CREATEAC_IMP":"创建账号","CORE_EMP_ADDEMP_IMP":"添加员工","CORE_EMP_TALENT_IMP":"人才库信息",},
	}
	moudle_type=moudle_type.split('-')
	filename="%s-%s.xlsx"%(moudle_type[1],moudle_type[2])
	moudle_type_ID=[]
	for k,v in moudle_dict["moudle"].items():
		if v==moudle_type[0]:
			moudle_type_ID.append(k)
	for k,v in moudle_dict["subMoudle"].items():
		if v==moudle_type[1]:
			moudle_type_ID.append(k)
	for k,v in moudle_dict["type"].items():
		if v==moudle_type[2]:
			moudle_type_ID.append(k)
	for k,v in moudle_dict["rNo"].items():
		if v==moudle_type[1]:
			moudle_type_ID.append(k)
	# print(moudle_type_ID)
	return moudle_type_ID,filename
def upload(moudle_type):
	try:
		cookie='userLoginType=60; HRX_SSO_COOKIE=185a5992-6fd8-44de-b167-531e13376fdc; loginToken=; HRX-WEB-SESSION=Sf9809fd3b7204612b942d857666daf050; SSOCLIENTSESSIONID=1af42264bd409a3b01ede9c099bf614b91fc55ea031861f9; userLanguage=ZHS'
		headers={"cookie":cookie,}
		#获取模板ID和文件名
		moudleID=get_moudleID(moudle_type)
		# print(moudleID)
		url_importExcel='https://hrx-sit.ctg.cn/corehr-api/corehr/common/batch/import/importExcel.do?rNo=%s'%moudleID[0][3]
		url_verifyExcelData='https://hrx-sit.ctg.cn/corehr-api/corehr/common/batch/import/verifyExcelData.do?rNo=%s'%moudleID[0][3]
		# cookie='userLoginType=60; HRX_SSO_COOKIE=68a3667a-5051-4594-ac25-bdc6c7a377db; loginToken=; HRX-WEB-SESSION=S230157ea1c8345c7b353cec4a0e737350; SSOCLIENTSESSIONID=680eb752ca97113b263d5c402358ae708c31d4afbeadafb6; userLanguage=ZHS'
		formdata={
		"moudle": moudleID[0][0],
		"subMoudle": moudleID[0][1],
		"type": moudleID[0][2],
		}
		files={'file':(moudleID[1],open('C:\\Users\\Administrator\\Downloads\\%s'%moudleID[1],'rb'),'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')}
		#上传文件
		r=requests.post(url=url_importExcel,data=formdata,files=files,headers=headers)
		# print(r.text)
		if r.json().get('responseMsg')=="请求成功":
			# print('模板上传成功')
			data=r.json()['data']
			#验证数据
			if r.json().get('responseMsg')=="请求成功":
				headers["content-type"]="application/json"
				r=requests.post(url=url_verifyExcelData,data=json.dumps(data),headers=headers)
				print(r.text)
			else:
				print(r.text)
		else:
			print(r.text)
	except Exception as e:
		print('未知错误%s'%e)
def login():
	#获取lt
	url_home='https://hrx-sit.ctg.cn/corehr/#/home'
	url_lt='http://testiam.ctg.cn:9020/asd/sso/login?service=http://testiam.ctg.cn:9020/asd/sso/oauth2.0/callbackAuthorize'
	# url_casService='http://testiam.ctg.cn:9020/asd/sso/login?service=http%3A%2F%2Ftestiam.ctg.cn%3A9020%2Fasd%2Fsso%2Foauth2.0%2FcallbackAuthorize'
	url_casService='http://testiam.ctg.cn:9020/asd/sso/oauth2.0/callbackAuthorize'

	r=requests.get(url=url_lt)
	print(r.headers['Set-Cookie'])
	lt=re.findall('name="lt" value="(.+?)"',r.text)
	print(lt[0])
	headers={
		"Cookie":r.headers['Set-Cookie'],
		"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36"
	}
	data={
		"username": "ihr25",
		"password": "",
		"authMode": "jdbc",
		"lt": lt[0],
		"execution": "e2s1",
		"_eventId": "submit",
	}
	r=requests.post(url=url_lt, headers=headers, data=data)
	print(r.status_code)
	return r

if __name__ == '__main__':
	login()
	# moudle=imput("请输入要测试的模板：")
	# for i in range(1,2):
	# 	print("批量导入第%s次："%i)
	# 	upload("员工-职务信息-更新")
	# upload("员工-职务信息-新增")
	# upload("员工-职务信息-更新数据")
	# upload("员工-职务信息-删除")

